import django.db.models
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect

# Create your views here.
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt

from hotel import models

@csrf_exempt
def login_view(request):
    if request.method=='POST':
        post = request.POST
        name = post.get("username")
        psd = post.get("password")
        u = models.Root.objects.filter(django.db.models.Q(root_name=name) & django.db.models.Q(root_psd=psd))
        #判断密码是否正确
        if u.exists():
            return render(request, "main.html", {})
        else:
            return render(request, "login.html", {})
    else:
        return render(request, "login.html", {})


def checkin_view(request):
    if request is not None:
        #获取信息
        name = request.POST.get("name")
        phone = request.POST.get("phone")
        place = request.POST.get("place")
        recard = request.POST.get("recard")
        money = request.POST.get("money")
        roomid = request.POST.get("roomid")

        nowtime = timezone.now()
        date = nowtime.strftime("%Y-%m-%d")
        o = models.Room.objects.filter(roomid=roomid).first()
        if o.roomstate == "入住":
            return HttpResponse("该房已入住，请重新选择房间")
        else:
            #添加数据
            obj = models.Check_in(name=name, phone=phone, place=place, recarde=recard,money=money,roomid=roomid,indate=date,outdate=date)
            obj1 = models.Room.objects.filter(roomid=roomid).update(roomstate="入住")
            obj.save()
            return HttpResponse("登记成功！！")
    else:
        return HttpResponse("请填写信息")


def checkout_view(request):
    room = request.POST.get("roomid")
    if room is not None:  #如果输入搜索room，开始搜索
        o = models.Check_in.objects.filter(roomid=room).first()
        return render(request, "outsearch.html", {'obj':o})
    else:
        return render(request, "outsearch.html", {})


def checkouto_view(request):
    #输入退房操作
    #获取房号
    roomid = request.POST.get("roomid")
    #删除数据
    i = models.Check_in.objects.filter(roomid=roomid).delete()
    #将房间状态改为未入住
    o = models.Room.objects.filter(roomid=roomid).first()
    if o.roomstate == "未入住":
        return HttpResponse(roomid+"该房无人入住，无法办理退房手续")
    else:
        o.roomstate = "未入住"
        o.save()
        #成功提示
        if i is not None:
            return HttpResponse(roomid+"退房成功，祝你旅途愉快")
        else:
            return HttpResponse("退房失败，请重试")


def userquery_view(request):
    #查询用户订单
    name = request.POST.get("name")
    i = models.Check_in.objects.filter(name=name)
    if i is not None:
        return render(request, "personsearch.html", {"obj":i})
    else:
        return HttpResponse("没有此用户订单")


def roomadd_view(request):
    roomid = request.POST.get("roomid")
    roomtype = request.POST.get("type")
    roomprice = request.POST.get("price")
    eid = models.Room.objects.filter(roomid=roomid).first()
    if eid is not None:
        return HttpResponse("该房已存在,添加失败")
    else:
        obj = models.Room(roomid=roomid, roomtype=roomtype, roomprice=roomprice)
        obj.save()
        return HttpResponse("添加成功")


def roomshow_view(request):
    #显示全部房间
    obj = models.Room.objects.all()
    return render(request, "addroom.html", {"obj":obj})


def roomdelete_view(request):
    # 输入退房操作
    # 获取房号
    roomid = request.POST.get("roomid")
    o = models.Room.objects.filter(roomid=roomid).first()
    if o.roomstate == "入住":
        return HttpResponse("该房间无法删除，请先退房")
        # 删除数据
    else:
        i = models.Room.objects.filter(roomid=roomid).delete()
        if i is not None:
            return HttpResponse(roomid + "房间删除成功")
        else:
            return HttpResponse("删除失败，请重试")


def roomchange_view(request):
    return None


def roomsearch_view(request):
    # 查询用户订单
    type = request.POST.get("roomtype")
    i = models.Room.objects.filter(roomtype=type)
    if i is not None:
        print(type)
        print(i)
        return render(request, "roomsearch.html", {"obj": i})
    else:
        return render(request, "roomsearch.html", {})


def in_view(request):
     id = request.POST.get("roomid")
     if id is not None:
         return render(request, "in.html", {"roomid":id})
     else:
         return render(request, "in.html", {})